var scrollFlag = true;
var scrollHeightOld = 0;
var endTimeArg;
function refresh() {
    scrollFlag = true;
    scrollHeightOld = 0;
    refreshTable();
}
function appendTabaleRow(data) {
    var dataArr = data;
    console.log("===dataArr.length======="+dataArr.length);
    if (dataArr.length > 0) {
        var pkstr = dataArr[dataArr.length - 1].pk;
        var endTimeObj = parse(pkstr.substring(pkstr.indexOf('_') + 1), 'yyyyMMddHHmmss'); //format(,'yyyy-MM-dd HH:mm:ss')
        endTimeObj.setSeconds(endTimeObj.getSeconds() + 1);
        endTimeArg = format(endTimeObj, 'yyyyMMddHHmmss');

    }else{
        if(endTimeArg && endTimeArg.length>1){
            var endTimeObj=parse(endTimeArg ,'yyyyMMddHHmmss'); //format(,'yyyy-MM-dd HH:mm:ss')
            endTimeObj.setSeconds(endTimeObj.getSeconds()+1);
            endTimeArg=format(endTimeObj,'yyyyMMddHHmmss') ;
        }
        /*

        */
    }
    $("#monitorTable").bootstrapTable("append",data);
}
function loadTabaleRow(data) {
    var dataArr = data;
    console.log("===dataArr.length======="+dataArr.length);
    if (dataArr.length > 0) {
        var pkstr = dataArr[dataArr.length - 1].pk;
        var endTimeObj = parse(pkstr.substring(pkstr.indexOf('_') + 1), 'yyyyMMddHHmmss'); //format(,'yyyy-MM-dd HH:mm:ss')
        endTimeObj.setSeconds(endTimeObj.getSeconds() + 1);
        endTimeArg = format(endTimeObj, 'yyyyMMddHHmmss');

    }else{
        if(endTimeArg && endTimeArg.length>1){
            var endTimeObj=parse(endTimeArg ,'yyyyMMddHHmmss'); //format(,'yyyy-MM-dd HH:mm:ss')
            endTimeObj.setSeconds(endTimeObj.getSeconds()+1);
            endTimeArg=format(endTimeObj,'yyyyMMddHHmmss') ;
        }
        /*

        */
    }
    $("#monitorTable").bootstrapTable("load",data);
}


function initTable() {
    var columns = monitorList.columnsInit();
    var table = monitorList.gridInit(columns);

    var start_time = $("#startTime").val().replaceAll("-", "").replaceAll(" ", "").replaceAll(":", "");

    var end_time = $("#endTime").val().replaceAll("-", "").replaceAll(" ", "").replaceAll(":", "");

    var args = {};
    args.iotCode = $("#iotCode").val();
    args.tableName = $("#tableName").val();
    args.startTime = start_time;  //20170918010000 ;
    args.endTime = end_time;      // 20170919010000   ;
    var paramStr = JSON.stringify(args);
    $$.ajax({
        url: '/rest/TimeDateViewBusiness/listData',
        data: args,
        success: function (data) {
            console.log("=data=="+JSON.stringify(data));
            loadTabaleRow(data);

        }
    });

}

function refreshTable() {


    var start_time = $("#startTime").val().replaceAll("-", "").replaceAll(" ", "").replaceAll(":", "");

    var end_time = $("#endTime").val().replaceAll("-", "").replaceAll(" ", "").replaceAll(":", "");

    var args = {};
    args.iotCode = $("#iotCode").val();
    args.tableName = $("#tableName").val();
    args.startTime = start_time;  //20170918010000 ;
    args.endTime = end_time;      // 20170919010000   ;
    var paramStr = JSON.stringify(args);
    $$.ajax({
        url: '/rest/TimeDateViewBusiness/listData',
        data: args,
        success: function (data) {
            console.log("=data=="+JSON.stringify(data));
            loadTabaleRow(data);

        }
    });

}

function scrollOnlyOne() {
    scrollFlag = true;
    appendtable();
}

function appendtable() {
    var end_time=$("#endTime").val().replaceAll("-","").replaceAll(" ","").replaceAll(":","");
    var args={};
    args.iotCode=$("#iotCode").val();
    args.tableName=$("#tableName").val();
    args.startTime=endTimeArg ;
    args.endTime=  end_time   ;
    if(endTimeArg>end_time){
        //alert("99");
        return ;
    }
    $$.ajax({
        url:'/rest/TimeDateViewBusiness/listData',
        data : args,
        success : function(data){
            appendTabaleRow(data);
        }
    });
}

$(function () {


    $('#iotCode').bind('keypress',function(e){
            refresh();

    });
    $('#startTime').bind('keypress',function(e){
        refresh();

    });
    $('#endTime').bind('keypress',function(e){
        refresh();

    });
    $('#btn_query').bind('keypress',function(e){
        refresh();

    });

    $("#rightCont").scroll(function () {

        var windowHeight = $(window).height();
        var documentHeight = $(document).height();
        var rightcontHeight = $("#rightCont").height();
        var rightcontScrollTop = $("#rightCont").scrollTop();

        var dataArea = $("#dataArea").height();
        var headerArea = $("#header").height();
        var toolbarArea = $("#toolbarArea").height();
        //console.log("=dataArea="+dataArea+"=toolbarArea="+toolbarArea+"=headerArea="+headerArea);
        //console.log("=windowHeight="+windowHeight+"=documentHeight="+documentHeight+"=rightcontHeight="+rightcontHeight+"==rightcontScrollTop=="+rightcontScrollTop);
        if ($("#rightCont").scrollTop() <= 0) {
            //console.log("滚动条已经到达顶部为==");
        }
        console.log(rightcontScrollTop+rightcontHeight);
         console.log(dataArea + toolbarArea);
        if (rightcontScrollTop + rightcontHeight >= dataArea + toolbarArea + 9) {
            console.log("滚动条==1==");
            //滚动条有时会多次触发，多次触发时，只执行一次。
            if (scrollFlag) {
                console.log("滚动条==2==");
                console.log("==rightcontScrollTop="+rightcontScrollTop);
                console.log("==scrollHeightOld="+scrollHeightOld);
                //滚动条向下移动时触发，向上移动时不触发
                if (rightcontScrollTop > scrollHeightOld) {
                    console.log("滚动条==3==");
                    setTimeout(scrollOnlyOne, 200);
                    scrollFlag = false;
                } else {
                    console.log("滚动条==4==");
                    console.log("==滚动条向上移动==");
                }
                scrollHeightOld = rightcontScrollTop;
            } else {
                console.log("滚动条==5==");
            }
        }
    });


});

$(function () {
    var params=$$.getQueryString();
    $("#iotCode").val(params.dataCode);
    $("#iotCode").focus();
    var start = new Date();
    start.setHours(0);
    start.setMinutes(0);
    start.setSeconds(0);
    var end = new Date();
    end.setDate(end.getDate() + 1);
    end.setHours(0);
    end.setMinutes(0);
    end.setSeconds(0);


    var start2 = {
        elem: '#startTime',
        type: 'datetime',
        value: start,
        min: '1900-01-01 00:00:00',
        max: '2099-06-16 23:59:59',
        isclear: true,
        istoday: true,
        choose: function (datas) {
            //end.min = datas; //开始日选好后，重置结束日的最小日期
            //end.start = datas //将结束日的初始值设定为开始日
        }
    };
    var end2 = {
        elem: '#endTime',
        type: 'datetime',
        value: end,
        min: '1900-01-01 00:00:00',
        max: '2099-06-16 23:59:59',
        isclear: true,
        istoday: true,
        choose: function (datas) {
            //end.min = datas; //开始日选好后，重置结束日的最小日期
            //end.start = datas //将结束日的初始值设定为开始日
        }
    };
    laydate.render(start2);
    laydate.render(end2);

    $("#startTime").val(format(start,'yyyy-MM-dd HH:mm:ss'));
    $("#endTime").val(format(end,'yyyy-MM-dd HH:mm:ss'));
    //渲染表格
    initTable();
    $("#btn_query").click(function () {
        refresh();
    });
    $("#searchReset").click(function () {
        $("#btn_dr").click();
    });
    //前一日
    $("#btn_qyr").click(function () {

        var today = new Date($("#startTime").val());
        //today.setHours(0);
        //today.setMinutes(0);
        //today.setSeconds(0);
        today.setDate(today.getDate() - 1);
        var tomorrow = new Date($("#endTime").val());
        //tomorrow.setHours(0);
        // tomorrow.setMinutes(0);
        // tomorrow.setSeconds(0);
        tomorrow.setDate(tomorrow.getDate() - 1);
        laydate.render({
            elem: '#startTime',
            type: 'datetime',
            format: 'yyyy-MM-dd HH:mm:ss',
            value: today,
            zIndex: 99999999
        });
        laydate.render({
            elem: '#endTime',
            type: 'datetime',
            format: 'yyyy-MM-dd HH:mm:ss',
            value: tomorrow,
            zIndex: 99999999
        });
        refresh();
    });
    //当日
    $("#btn_dr").click(function () {

        var today = new Date();
        today.setHours(0);
        today.setMinutes(0);
        today.setSeconds(0);

        var tomorrow = new Date();
        tomorrow.setDate(today.getDate() + 1);
        tomorrow.setHours(0);
        tomorrow.setMinutes(0);
        tomorrow.setSeconds(0);
        laydate.render({
            elem: '#startTime',
            type: 'datetime',
            format: 'yyyy-MM-dd HH:mm:ss',
            value: today,
            zIndex: 99999999
        });
        laydate.render({
            elem: '#endTime',
            type: 'datetime',
            format: 'yyyy-MM-dd HH:mm:ss',
            value: tomorrow,
            zIndex: 99999999
        });
        refresh();
    });
    //后一日
    $("#btn_hyr").click(function () {

        var today = new Date($("#startTime").val());
        // today.setHours(0);
        //today.setMinutes(0);
        // today.setSeconds(0);
        today.setDate(today.getDate() + 1);
        var tomorrow = new Date($("#endTime").val());
        // tomorrow.setHours(0);
        // tomorrow.setMinutes(0);
        // tomorrow.setSeconds(0);
        tomorrow.setDate(tomorrow.getDate() + 1);
        laydate.render({
            elem: '#startTime',
            type: 'datetime',
            format: 'yyyy-MM-dd HH:mm:ss',
            value: today,
            zIndex: 99999999
        });
        laydate.render({
            elem: '#endTime',
            type: 'datetime',
            format: 'yyyy-MM-dd HH:mm:ss',
            value: tomorrow,
            zIndex: 99999999
        });
        refresh();
    });
    //前一时
    $("#btn_qys").click(function () {

        var today = new Date($("#startTime").val());
        today.setHours(today.getHours() - 1);
        today.setMinutes(0);
        today.setSeconds(0);

        var tomorrow = new Date($("#endTime").val());
        tomorrow.setHours(tomorrow.getHours() - 1);
        tomorrow.setMinutes(0);
        tomorrow.setSeconds(0);
        laydate.render({
            elem: '#startTime',
            type: 'datetime',
            format: 'yyyy-MM-dd HH:mm:ss',
            value: today,
            zIndex: 99999999
        });
        laydate.render({
            elem: '#endTime',
            type: 'datetime',
            format: 'yyyy-MM-dd HH:mm:ss',
            value: tomorrow,
            zIndex: 99999999
        });
        refresh();
    });
    //当时
    $("#btn_ds").click(function () {

        var today = new Date();
        today.setMinutes(0);
        today.setSeconds(0);
        var tomorrow = new Date();
        tomorrow.setHours(tomorrow.getHours() + 1);
        tomorrow.setMinutes(0);
        tomorrow.setSeconds(0);
        laydate.render({
            elem: '#startTime',
            type: 'datetime',
            format: 'yyyy-MM-dd HH:mm:ss',
            value: today,
            zIndex: 99999999
        });
        laydate.render({
            elem: '#endTime',
            type: 'datetime',
            format: 'yyyy-MM-dd HH:mm:ss',
            value: tomorrow,
            zIndex: 99999999
        });
        refresh();
    });
    //后一时
    $("#btn_hys").click(function () {

        var today = new Date($("#startTime").val());
        today.setHours(today.getHours() + 1);
        today.setMinutes(0);
        today.setSeconds(0);

        var tomorrow = new Date($("#endTime").val());
        tomorrow.setHours(tomorrow.getHours() + 1);
        tomorrow.setMinutes(0);
        tomorrow.setSeconds(0);
        laydate.render({
            elem: '#startTime',
            type: 'datetime',
            format: 'yyyy-MM-dd HH:mm:ss',
            value: today,
            zIndex: 99999999
        });
        laydate.render({
            elem: '#endTime',
            type: 'datetime',
            format: 'yyyy-MM-dd HH:mm:ss',
            value: tomorrow,
            zIndex: 99999999
        });
        refresh();
    });
})
function formatPk(pk){
    var retStr=pk.substring(pk.indexOf("_")+1);
    //console.log(format(parse(retStr,'yyyyMMddHHmmss'),'yyyy-MM-dd HH:mm:ss'));
    return format(parse(retStr,'yyyyMMddHHmmss'),'yyyy-MM-dd HH:mm:ss');
}
;(function (undefined) {
    "use strict"
    var _global;

    function result(args, fn) {
        var argsArr = Array.prototype.slice.call(args);
        if (argsArr.length > 0) {
            return argsArr.reduce(fn);
        } else {
            return 0;
        }
    }

    var monitorList = {
        columnsInit: function () {
            return [{
                field: 'pk',
                title: '序号',
                width: '2%',
                align: 'center',
                formatter: function (value, row, index) {
                    return     index+1 ;
                }
            },{
                field: 'pk',
                title: '时间',
                width: '15%',
                align: 'center',
                formatter: function (value, row, index) {
                     return    formatPk(value);
                }
            }, {
                field: 'o',
                title: '原始值'
            }, {
                field: 'v',
                title: '计算值'
            }, {
                field: 'b',
                title: '差值'
            }, {
                field: 's',
                title: '时间差'
            }, {
                field: 'st',
                title: '创建时间',
                formatter: function (value, row, index) {
                    if(value.length==14){
                       return format(parse(value,'yyyyMMddHHmmss'),'yyyy-MM-dd HH:mm:ss');
                    }else{
                        return "-";
                    }


                }
            }]
        },
        gridInit: function (columns) {
            return $("#monitorTable").bootstrapTable({
                url: '',
                columns: columns,
                striped: 'true',
                classes: 'table table-hover qxttable',
                pagination: false,
                pageNumber: 1,
                pageSize: 10,
                pageList: [
                	10, 20, 50, 100
                ],
                onPostBody: function () {
                    $(".opt-dropdown").each(function (index, data) {
                        var _this = this;
                        $(this).hover(function () {
                            $(_this).find(".opt-dropdown-menu").show();
                        }, function () {
                            $(_this).find(".opt-dropdown-menu").hide();
                        })
                    });
                }
            })

        },
        dataInit: function () {
            return []
        }
    }

    // 将插件对象暴露给全局对象
    _global = (function () {
        return this || (0, eval)('this');
    }());
    if (typeof module !== "undefined" && module.exports) {
        module.exports = monitorList;
    } else if (typeof define === "function" && define.amd) {
        define(function () {
            return monitorList;
        });
    } else {
        !('plugin' in _global) && (_global.monitorList = monitorList);
    }
}());